# -*- coding: utf-8 -*-
# @Author   : zxc19
# @Time     : 2023/7/7 10:18
# @FileName : read_case.py
# @Desc     : 请输入功能模块描述
from pathlib import Path
from typing import Union, List, Tuple

from config import settings
from utils.yaml_utils import YamlUtil


class ReadCaseData(object):
    @staticmethod
    def read_by_yaml(file_path: Union[Path, str]):
        """
        读取yaml格式用例，默认直接读取TEST_DATA_DIR目录下的文件
        :param file_path: 用例文件路径（默认是TEST_DATA_DIR目录下的，无需指定全路径）
        :return:
        """
        if not Path(file_path).is_absolute():
            file_path = settings.TEST_DATA_DIR.joinpath(file_path)
        if not file_path.exists():
            raise ValueError(f'文件【{file_path}】不存在')
        yaml_data = YamlUtil(file_path).read_yaml()
        # 预处理用例数据
        if yaml_data is None:
            raise ValueError('未读取到测试用例数据')

        for case_data in yaml_data:
            pass
            # 拼接URL地址
            #     if case_data.get("url地址") is not None:
            #         case_data["url地址"] = build_full_url(settings.BASE_URL, case_data["url地址"])
            #     # 拼接上传文件的地址
            #     if case_data.get("files") is not None:
            #         case_data['files'] = ReadCaseData.handle_files(case_data.get("files"))
        return yaml_data

    @staticmethod
    def handle_files(files: str | List | Tuple) -> str | List:
        """
        处理用例中上传文件的路径
        :param files:
        :return:
        """
        if isinstance(files, List | Tuple):
            new_files = []
            for file in files:
                if file:
                    tmp_files = ReadCaseData.handle_files(file)
                    new_files.append(tmp_files)
            return new_files
        else:
            return str(settings.TEST_FILE_DIR.joinpath(str(Path(files))))

